package com.ftf.tp.blog.application.service.representation.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ftf.tp.blog.application.assembler.ArticleAssembler;
import com.ftf.tp.blog.application.assembler.ColumnAssembler;
import com.ftf.tp.blog.application.assembler.TagAssembler;
import com.ftf.tp.blog.application.dto.ArticlePageDTO;
import com.ftf.tp.blog.application.service.representation.ArticleRepresentation;
import com.ftf.tp.blog.application.vo.ArticleIndexVO;
import com.ftf.tp.blog.application.vo.ArticleInfoVO;
import com.ftf.tp.blog.domain.article.dos.ArticleConfigureDO;
import com.ftf.tp.blog.domain.article.dos.ArticleIndexDO;
import com.ftf.tp.blog.domain.article.dos.ArticleInfoDO;
import com.ftf.tp.blog.domain.article.repository.ArticleRepository;
import com.ftf.tp.common.constants.RedisConstant;
import com.ftf.tp.common.core.redis.utils.RedissonHelper;
import org.redisson.client.protocol.ScoredEntry;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tanghao
 * @date 2022/8/9
 */
@Service
public class ArticleRepresentationImpl implements ArticleRepresentation {

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private RedissonHelper redissonHelper;

    /**
     * 分页查询文章列表
     *
     * @param articlePageDTO 分页查V条件
     */
    @Override
    public List<ArticleIndexVO> pageBlog(ArticlePageDTO articlePageDTO) {
        List<Long> idList = null;
        if (articlePageDTO.getIsRecommend()) {
            //todo 通过推荐方法获取id
            idList = articleRepository.getIdListRand();
        } else if (articlePageDTO.getIsHot()) {
            int page = articlePageDTO.getPage();
            int size = articlePageDTO.getSize();
            //获取热榜前50的id
            Collection<ScoredEntry<Object>> entries = redissonHelper.entryRange(RedisConstant.REDIS_KEY_BLOG_HOT_VALUE,
                    0, articlePageDTO.getSize());
            //获取热榜信息
            List<ArticleIndexDO> articleIndexVOList = articleRepository.hotRankInfo(entries.stream().map(entrie ->
                    Long.valueOf((String) entrie.getValue())).collect(Collectors.toList()));

            Map<Long, ArticleIndexDO> idMap = articleIndexVOList.stream()
                    .collect(Collectors.toMap(ArticleIndexDO::getId, v -> v, (a, b) -> a));
            entries.forEach(entrie -> {
                ArticleIndexDO articleIndexDO = idMap.get(Long.valueOf(String.valueOf(entrie.getValue())));
                articleIndexDO.setHotValue(Long.valueOf(String.valueOf(entrie.getScore())));
            });
            return ArticleAssembler.INSTANCE.doToVOList(articleIndexVOList);
        } else if (articlePageDTO.getFollow()) {
            //todo 通过关注列表获取id
            idList = articleRepository.getIdListRand();
        } else {
            //如果没有条件就去数据库随机取10个
            idList = articleRepository.getIdListRand();
        }
        //根据id从es从获取数据
        if (CollectionUtils.isNotEmpty(idList)) {
            return ArticleAssembler.INSTANCE.doToVOList(articleRepository.queryArticleMainInfo(idList));
        }
        return null;
    }


    /**
     * 根据文章id获取文章详情
     *
     * @param articleId 文章id
     * @return 文章详情
     */
    @Override
    public ArticleInfoVO getArticleInfoById(Long articleId) {
        ArticleInfoDO articleInfoDO = articleRepository.getArticleInfoById(articleId);
        Long supportByBlogId = articleRepository.getSupportByArticleId(articleId);
        Long hotValueByBlogId = articleRepository.getHotValueByArticleId(articleId);
        Long lookByBlogId = articleRepository.getLookByArticleId(articleId);
        articleInfoDO.setSupportAmount(supportByBlogId);
        articleInfoDO.setHotValue(hotValueByBlogId);
        articleInfoDO.setCommentAmount(0L);
        articleInfoDO.setCollectionAmount(0L);
        articleInfoDO.setLookAmount(lookByBlogId);
        ArticleConfigureDO blogConfigureById = articleRepository.getArticleConfigureById(articleId);
        articleInfoDO.setTagDOList(blogConfigureById.getTagDOList());
        articleInfoDO.setColumnDOList(blogConfigureById.getColumnDOList());
        ArticleInfoVO articleInfoVO = ArticleAssembler.INSTANCE.doToVO(articleInfoDO);
        articleInfoVO.setTagVOList(TagAssembler.INSTANCE.doToVoList(articleInfoDO.getTagDOList()));
        articleInfoVO.setColumnVOList(ColumnAssembler.INSTANCE.doToVoList(articleInfoDO.getColumnDOList()));
        return articleInfoVO;
    }
}
